{include file="public/header" /}
<span class="layui-breadcrumb">
  <a href="javascript:;">分类</a>
  <a><cite>列表</cite></a>
</span>
<div class="layui-fluid">
    <div class="layui-card">
      <div class="layui-card-body">
        <div class="layui-form">

          <div class="layui-form-item" style='margin-top:20px'>
             <!-- <button class="layui-btn layui-btn-normal layui-btn-sm" onclick="insert_edit()">添加</button> -->
            <div class="layui-inline">
              <label class="layui-form-label">分类名称</label>
              <div class="layui-input-inline">
                <input type="text" class="layui-input" name='cname' placeholder="请输入分类名称" >
              </div>
            </div>
            <div class="layui-inline">
              <button type="button" class="layui-btn layui-btn-primary layui-btn-sm" onclick="sear()">查询</button>
              <a href="{:url('Classify/index')}?nav=classify-classifylist" class="layui-btn layui-btn-sm layui-btn-danger">清空</a>
            </div>
          </div>

          <table class="layui-hide" id="test" lay-filter="test"></table>

        </div>

      </div>  
    </div>
</div>

{include file="/public/footer" /}
<script>
var table

layui.use('table', function(){
 table = layui.table;
  
  table.render({
    elem: '#test'
    ,url:'/admin/Classify/get_data/'
    ,toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
    ,limit:15
    ,cols: [[
      {field:'cname', title:'分类名称'}
      ,{field:'alias_name', title:'分类别名(H1)'}
      ,{field:'load', title:'路径'}
      ,{fixed: 'right', title:'操作',templet: function(res){
       /* var str='<button type="button" class="layui-btn layui-btn-danger layui-btn-xs" onclick="del('+res.id+')">删除</button>';*/
       
        
        var str="<a href='/"+res.load_url+"' class='layui-btn layui-btn-xs' target='_blank' >访问链接</a>";
           str+="<a href='{:url('Classify/edit_class')}?nav=classify-classifylist&id="+res.id+"' class='layui-btn layui-btn-xs' target='_blank' >编辑</a>";
           str+="<a href='{:url('Classify/tdk')}?nav=classify-classifylist&id="+res.id+"' class='layui-btn layui-btn-xs' target='_blank' >seo编辑</a>";
        if(res.route_id==2){
          str+="<a href='{:url('Classify/enedit')}?nav=classify-classifylist&id="+res.id+"' class='layui-btn layui-btn-xs' target='_blank' >内容编辑</a>";
        }
        if(res.route_id==3){
          str+="<a href='{:url('Classify/dan')}?nav=classify-classifylist&id="+res.id+"' class='layui-btn layui-btn-xs' target='_blank' >页面编辑</a>";
        }
              
        return str;  
      }}
    ]]

    ,page: true
    
  });
});

  /*查询*/
  function sear(){
    var cname=$.trim($("input[name='cname']").val());
    if(cname == '') {return false;}

    table.reload('test', {
      url: '/admin/Classify/get_data/',
      where: { //设定异步数据接口的额外参数，任意设
          'sear': cname 
      },
      page: {
        curr: 1 //重新从第 1 页开始
      }
    }); 
  }
 
  
  function del(id){
    layer.confirm('！该操作会删除该类及其子类', {
      btn: ['确定','取消'] //按钮
    }, function(){
      layer.load();
      $.post("{:url('Classify/del')}",{id:id},function(res){
          if(res.status=='0'){
              layer.closeAll('loading');
              layer.msg(res.msg);
          }
          if(res.status=='1'){
              layer.msg(
              res.msg,{icon: 1,time: 600},
              function(){
                layer.closeAll('loading');
                location.href=location.href;
              });
          }
      })
    });
  }

  

   /*添加 或 修改 找change.html*/
 function insert_edit(){
    layer.open({
      type: 2,
      title:'添加',
      area: ['50%', '50%'],
      maxmin: true,
      content: 'change.html',
      btn: ['提交'],
      yes: function (index,layero) {
          do_insert_edit(index,layero);//执行 
      }
    }); 
 }
 
/*执行添加或修改*/
 function do_insert_edit(index,layero){
      layer.load();
      var body = layer.getChildFrame('body', index);//子页面dom
      /*获取子页面输入内容*/
      var cname=$.trim(body.find("input[name='cname']").val());
      var newname=$.trim(body.find("input[name='newname']").val());
      var quiz0=$.trim(body.find("select[name='quiz0'] option:selected").val());
      var quiz1=$.trim(body.find("select[name='quiz1'] option:selected").val());
      var quiz2=$.trim(body.find("select[name='quiz2'] option:selected").val());

      if(cname==''){
          layer.closeAll('loading');
          layer.msg('请填写分类名称');
          return false;
      }
      /*在这里进行数据分析确定父id为多少 等级为多少 若quiz1为0则fid为0 level为1,若quiz1不为0*/
      if(quiz0=='0'){
        var fid=0;
        var level=1;
      }else{
        if(quiz1=='0'){
          var fid=quiz0;
          var level=2;
        }else{
          if(quiz2=='0'){
            var fid=quiz1;
            var level=3;
          }else{
            var fid=quiz2;
            var level=4;
          }
        }
      }

      var url="{:url('Classify/change')}";
      $.post(url,{cname:cname,fid:fid,level:level,newname:newname},function(res){
          if(res.status=='0'){
              layer.closeAll('loading');
              layer.msg(res.msg);
          }
          if(res.status=='1'){
              layer.msg(
              res.msg,{time: 1500 },
              function(){
                layer.closeAll();
                location.href=location.href;
              });
          }
      }) 
 }


</script>
